package com.xbn.xbnmall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xbn.xbnmall.product.entity.AttrEntity;
import com.xbn.xbnmall.product.service.AttrAttrgroupRelationService;
import com.xbn.xbnmall.product.service.AttrService;
import com.xbn.xbnmall.product.service.CategoryService;
import com.xbn.xbnmall.product.vo.AttrGroupRelationVo;
import com.xbn.xbnmall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xbn.xbnmall.product.entity.AttrGroupEntity;
import com.xbn.xbnmall.product.service.AttrGroupService;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.R;



/**
 * 属性分组
 *
 * @author XBN
 * @email 78742541@qq.com
 * @date 2020-06-26 15:16:04
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;


    /*
    * 获取某个分类下所有的分组以及这些分组下的所有属性
    * */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
      List<AttrGroupWithAttrsVo> vos=  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
    return R.ok().put("data",vos);
    }



    /*
     * 新增关联关系
     * */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos ){
        relationService.saveBatch(vos);
        return R.ok();
    }

    /*
    * 查找某个属性分组下关联的属性
    * */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attegroundId){
      List<AttrEntity> attrs= attrService.getRelationAttr(attegroundId);
        return R.ok().put("data",attrs);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attegroundId,
                            @RequestParam Map<String, Object> params){
      PageUtils page= attrService.getNoRelationAttr(params,attegroundId);
        return R.ok().put("page",page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    /*
    * 根据三级分类查找分组
    * */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R listByCatelogId(@RequestParam Map<String, Object> params,
                             @PathVariable Long catelogId){
    //    PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //找到从一级分类到三级分类的完整路径
        Long categoryId=attrGroup.getCatelogId();
      Long[] path=categoryService.findCatelogPath(categoryId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /*
     * 删除关联关系
     * */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
 //   @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
